package rursu.view;

import rursu.business.MovieApplication;
import rursu.business.ProgressListener;
import rursu.business.R;
import rursu.business.Settings;
import rursu.business.pojo.Movie;
import rursu.dal.managers.DataBaseManager;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends FragmentActivity implements ProgressListener {

	private static final String LOGGER_TAG = MainActivity.class.getCanonicalName();

	private ActionBar actionBar; 
	private ProgressBar progress;
	
	private static final String MOVIE_DETAILS_TAG = "MOVIE_DETAILS_FRAGMENT";	

	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		
		Intent intent = getIntent();
		
		// prepare action bar
		actionBar = this.getSupportActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.actionbar);
		
		View view = actionBar.getCustomView();
		progress = (ProgressBar) view.findViewById(R.id.actionbar_progress);
		
		// initialize Data Provider Manager progress listener
		((MovieApplication)getApplication()).setProgressListener(this);
		
		if (((MovieApplication)getApplication()).getDataProviderManager().getDALManager() == null) {
			DataBaseManager applicationDALManager = new DataBaseManager(this);
			((MovieApplication)getApplication()).getDataProviderManager().setDALManager(applicationDALManager);
		}
		
		// init preferences
		Settings settings = new Settings(this.getApplicationContext());
		((MovieApplication)getApplication()).getDataProviderManager().setSettings(settings);
		
		
		if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
			
			String query = null;
			query = intent.getStringExtra(SearchManager.QUERY);
			
			SearchMovieFragment fragment = new SearchMovieFragment();
			getSupportFragmentManager().beginTransaction().add(R.id.container_view, fragment).commit();
			fragment.setQuery(query);
			
		} else {
			
			if (intent.getAction().equals(Intent.ACTION_MAIN)) {
				MoviesFragment fragment = new MoviesFragment();
				getSupportFragmentManager().beginTransaction().add(R.id.container_view, fragment).commit();
			}
		}
	}
	
	public void showMovieDetails( Movie movie ) {
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		
		MovieDetailsFragment details = (MovieDetailsFragment) getSupportFragmentManager().findFragmentByTag(MOVIE_DETAILS_TAG);
		
		if ( details != null ) {
			
			Log.v(LOGGER_TAG, "Old details fragment object is using to show details");
			details.setMovie( movie );
			
		} else {
			
			Log.v(LOGGER_TAG, "New details fragment object is using to show details");
			details = new MovieDetailsFragment();
			details.setMovie(movie);
			
			ft.add(details, MOVIE_DETAILS_TAG);			
		}
		
		ft.replace(R.id.container_view, details, MOVIE_DETAILS_TAG);
		ft.addToBackStack(null);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		
		ft.commit();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		// Handle item selection
		switch (item.getItemId()) {

		case R.id.menu_item_preferences: {
			Intent intent = new Intent(this,
					MoviePreferencesActivity.class);
			startActivityForResult(intent, 0);
			return true;
		}		
		case R.id.menu_item_search: {
			
			onSearchRequested();
			return true;
		}

		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private void showSpinner(boolean show) {
		
		if (show) {			
	        progress.setVisibility(View.VISIBLE);
		} else {
	        progress.setVisibility(View.INVISIBLE);
		}
	}

	public synchronized void progressContinue() {
		showSpinner(true);
	}

	public synchronized void progressFinished() {
		showSpinner(false);
	}
	
}